package jmvm.schemes;

import java.awt.Point;
import java.util.ArrayList;

import jmvm.components.*;
import jmvm.gui.GuiLine;
import jmvm.gui.GuiScheme;


public class UpravOper {
	
	
	public static DC dc; 		// Izlazi ovog dekodera od 0h do 54h predstavljaju signale V00 do V54
	
	public static OR mxMDR0, MDRout, ldMAR, incMar, eMAR, ldDWH, DWout, ldPC, PCout, IRPOMout, IRJAout,
	IRDAout, wrGPR, GPRout, SPout, ldCW, ldCW1, ADDout, ALUout, ldAB, ABout,
	ldBB, ldAW, AWout, ldBW, ldN, ldZ, ldC, ldV, clPSWI, clPSWT, ldBR,
	IVTPout, UINTout;
	
	////////////
	//////////// Grafika
	////////////
	
	public static GuiScheme gui;

	public static GuiScheme getGui() {
		return gui;
	}
	
	public static void gui() {

		GuiLine line; // Pomocna promenljiva
		gui = new GuiScheme("jmvm/jmvm/images/UpravOper.png");
		
		ArrayList<ArrayList<Point>> sections;
		ArrayList<Point> points;
		
		// 
		// 
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(60,140));
		points.add(new Point(82,140));
		sections.add(points);
		line=new GuiLine(sections,UpravljackaJedinica.CW_0_7Bits.getOut(0));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(60,154));
		points.add(new Point(82,154));
		sections.add(points);
		line=new GuiLine(sections,UpravljackaJedinica.CW_0_7Bits.getOut(1));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(60,168));
		points.add(new Point(82,168));
		sections.add(points);
		line=new GuiLine(sections,UpravljackaJedinica.CW_0_7Bits.getOut(2));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(60,182));
		points.add(new Point(82,182));
		sections.add(points);
		line=new GuiLine(sections,UpravljackaJedinica.CW_0_7Bits.getOut(3));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(61,196));
		points.add(new Point(83,196));
		sections.add(points);
		line=new GuiLine(sections,UpravljackaJedinica.CW_0_7Bits.getOut(4));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(61,209));
		points.add(new Point(83,209));
		sections.add(points);
		line=new GuiLine(sections,UpravljackaJedinica.CW_0_7Bits.getOut(5));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(61,225));
		points.add(new Point(82,225));
		sections.add(points);
		line=new GuiLine(sections,UpravljackaJedinica.CW_0_7Bits.getOut(6));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(148,73));
		points.add(new Point(218,73));
		sections.add(points);
		line=new GuiLine(sections,dc.getOut(0x0));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(148,89));
		points.add(new Point(218,89));
		sections.add(points);
		line=new GuiLine(sections,dc.getOut(0x1));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(149,105));
		points.add(new Point(219,105));
		sections.add(points);
		line=new GuiLine(sections,dc.getOut(0x2));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(149,218));
		points.add(new Point(219,218));
		sections.add(points);
		line=new GuiLine(sections,dc.getOut(0x54));
		gui.addLine(line);

		//
		//

	}
	/////////////
	/////////////
	/////////////
	
	public static void init(){
		
		dc = new DC(7);	
		
		mxMDR0 = new OR(8);
		MDRout = new OR(10);
		ldMAR = new OR(7); 
		incMar = new OR(2); 
		eMAR = new OR(2); 
		ldDWH = new OR(2); 
		DWout = new OR(4); 
		ldPC = new OR(4); 
		PCout = new OR(3); 
		IRPOMout = new OR(2); 
		IRJAout = new OR(2);
		IRDAout = new OR(4); 
		wrGPR = new OR(2); 
		GPRout = new OR(5); 
		SPout = new OR(3); 
		ldCW = new OR(4); 
		ldCW1 = new OR(5); 
		ADDout = new OR(3); 
		ALUout = new OR(8); 
		ldAB = new OR(2); 
		ABout = new OR(2);
		ldBB = new OR(3); 
		ldAW = new OR(4); 
		AWout = new OR(6); 
		ldBW = new OR(3); 
		ldN = new OR(3);  
		ldC = new OR(7); 
		ldV = new OR(6); 
		clPSWI = new OR(2); 
		clPSWT = new OR(2); 
		ldBR = new OR(6);
		IVTPout = new OR(2); 
		UINTout = new OR(4);
		
		

	}
	
	public static void connect(){
		
		dc.setInputPin(0, UpravljackaJedinica.CW_0_7Bits.getOut(0));
		dc.setInputPin(1, UpravljackaJedinica.CW_0_7Bits.getOut(1));
		dc.setInputPin(2, UpravljackaJedinica.CW_0_7Bits.getOut(2));
		dc.setInputPin(3, UpravljackaJedinica.CW_0_7Bits.getOut(3));
		dc.setInputPin(4, UpravljackaJedinica.CW_0_7Bits.getOut(4));
		dc.setInputPin(5, UpravljackaJedinica.CW_0_7Bits.getOut(5));
		dc.setInputPin(6, UpravljackaJedinica.CW_0_7Bits.getOut(6));
		dc.setE(new Pin(true));
		
		mxMDR0.setInputPin(0, dc.getOut(0x22));
		mxMDR0.setInputPin(1, dc.getOut(0x25));
		mxMDR0.setInputPin(2, dc.getOut(0x26));
		mxMDR0.setInputPin(3, dc.getOut(0x40));
		mxMDR0.setInputPin(4, dc.getOut(0x41));
		mxMDR0.setInputPin(5, dc.getOut(0x45));
		mxMDR0.setInputPin(6, dc.getOut(0x46));
		mxMDR0.setInputPin(7, dc.getOut(0x53));
		
		MDRout.setInputPin(0, dc.getOut(0x03));
		MDRout.setInputPin(1, dc.getOut(0x05));
		MDRout.setInputPin(2, dc.getOut(0x07));
		MDRout.setInputPin(3, dc.getOut(0x08));
		MDRout.setInputPin(4, dc.getOut(0x0D));
		MDRout.setInputPin(5, dc.getOut(0x0E));
		MDRout.setInputPin(6, dc.getOut(0x0F));
		MDRout.setInputPin(7, dc.getOut(0x17));
		MDRout.setInputPin(8, dc.getOut(0x42));
		MDRout.setInputPin(9, dc.getOut(0x43));
		
		ldMAR.setInputPin(0, dc.getOut(0x01));
		ldMAR.setInputPin(1, dc.getOut(0x0B));
		ldMAR.setInputPin(2, dc.getOut(0x0C));
		ldMAR.setInputPin(3, dc.getOut(0x10));
		ldMAR.setInputPin(4, dc.getOut(0x13));
		ldMAR.setInputPin(5, dc.getOut(0x28));
		ldMAR.setInputPin(6, dc.getOut(0x29));
		
		incMar.setInputPin(0, dc.getOut(0x0E));
		incMar.setInputPin(1, dc.getOut(0x53));
		
		eMAR.setInputPin(0, dc.getOut(0x02));
		eMAR.setInputPin(1, dc.getOut(0x23));
		
		ldDWH.setInputPin(0, dc.getOut(0x0D));
		ldDWH.setInputPin(1, dc.getOut(0x0E));
		
		DWout.setInputPin(0, dc.getOut(0x10));
		DWout.setInputPin(1, dc.getOut(0x18));
		DWout.setInputPin(2, dc.getOut(0x3A));
		DWout.setInputPin(3, dc.getOut(0x44));
		
		ldPC.setInputPin(0, dc.getOut(0x3A));
		ldPC.setInputPin(1, dc.getOut(0x3D));
		ldPC.setInputPin(2, dc.getOut(0x3E));
		ldPC.setInputPin(3, dc.getOut(0x44));
		
		PCout.setInputPin(0, dc.getOut(0x01));
		PCout.setInputPin(1, dc.getOut(0x16));
		PCout.setInputPin(2, dc.getOut(0x41));
		
		IRPOMout.setInputPin(0, dc.getOut(0x3C));
		IRPOMout.setInputPin(1, dc.getOut(0x47));
		
		IRJAout.setInputPin(0, dc.getOut(0x3E));
		IRJAout.setInputPin(1, dc.getOut(0x4F));
		
		IRDAout.setInputPin(0, dc.getOut(0x0C));
		IRDAout.setInputPin(1, dc.getOut(0x12));
		IRDAout.setInputPin(2, dc.getOut(0x14));
		IRDAout.setInputPin(3, dc.getOut(0x19));
		
		wrGPR.setInputPin(0, dc.getOut(0x24));
		wrGPR.setInputPin(1, dc.getOut(0x27));
		
		GPRout.setInputPin(0, dc.getOut(0x09));
		GPRout.setInputPin(1, dc.getOut(0x0A));
		GPRout.setInputPin(2, dc.getOut(0x0B));
		GPRout.setInputPin(3, dc.getOut(0x11));
		GPRout.setInputPin(4, dc.getOut(0x15));
		
		SPout.setInputPin(0, dc.getOut(0x28));
		SPout.setInputPin(1, dc.getOut(0x29));
		SPout.setInputPin(2, dc.getOut(0x2F));
		
		ldCW.setInputPin(0, dc.getOut(0x11));
		ldCW.setInputPin(1, dc.getOut(0x16));
		ldCW.setInputPin(2, dc.getOut(0x4D));
		ldCW.setInputPin(3, dc.getOut(0x50));
		
		ldCW1.setInputPin(0, dc.getOut(0x12));
		ldCW1.setInputPin(1, dc.getOut(0x14));
		ldCW1.setInputPin(2, dc.getOut(0x15));
		ldCW1.setInputPin(3, dc.getOut(0x3C));
		ldCW1.setInputPin(4, dc.getOut(0x4E));
		
		ADDout.setInputPin(0, dc.getOut(0x13));
		ADDout.setInputPin(1, dc.getOut(0x3D));
		ADDout.setInputPin(2, dc.getOut(0x50));
		
		ALUout.setInputPin(0, dc.getOut(0x31));
		ALUout.setInputPin(1, dc.getOut(0x32));
		ALUout.setInputPin(2, dc.getOut(0x33));
		ALUout.setInputPin(3, dc.getOut(0x34));
		ALUout.setInputPin(4, dc.getOut(0x35));
		ALUout.setInputPin(5, dc.getOut(0x36));
		ALUout.setInputPin(6, dc.getOut(0x37));
		ALUout.setInputPin(7, dc.getOut(0x38));
		
		ldAB.setInputPin(0, dc.getOut(0x1F));
		ldAB.setInputPin(1, ALUout.getOut(0));
		
		ABout.setInputPin(0, dc.getOut(0x22));
		ABout.setInputPin(1, dc.getOut(0x24));
		
		ldBB.setInputPin(0, dc.getOut(0x09));
		ldBB.setInputPin(1, dc.getOut(0x17));
		ldBB.setInputPin(2, dc.getOut(0x4F));
		
		ldAW.setInputPin(0, dc.getOut(0x21));
		ldAW.setInputPin(1, dc.getOut(0x2B));
		ldAW.setInputPin(2, dc.getOut(0x2D));
		ldAW.setInputPin(3, dc.getOut(0x2F));
		
		AWout.setInputPin(0, dc.getOut(0x26));
		AWout.setInputPin(1, dc.getOut(0x27));
		AWout.setInputPin(2, dc.getOut(0x2C));
		AWout.setInputPin(3, dc.getOut(0x2E));
		AWout.setInputPin(4, dc.getOut(0x30));
		AWout.setInputPin(5, dc.getOut(0x53));
		
		ldBW.setInputPin(0, dc.getOut(0x0A));
		ldBW.setInputPin(1, dc.getOut(0x18));
		ldBW.setInputPin(2, dc.getOut(0x19));
		
		ldN.setInputPin(0, dc.getOut(0x20));
		ldN.setInputPin(1, dc.getOut(0x52));
		ldN.setInputPin(2, dc.getOut(0x54));
		
		ldC.setInputPin(0, dc.getOut(0x20));
		ldC.setInputPin(1, dc.getOut(0x31));
		ldC.setInputPin(2, dc.getOut(0x32));
		ldC.setInputPin(3, dc.getOut(0x33));
		ldC.setInputPin(4, dc.getOut(0x34));
		ldC.setInputPin(5, dc.getOut(0x39));
		ldC.setInputPin(6, dc.getOut(0x51));
		
		ldV.setInputPin(0, dc.getOut(0x20));
		ldV.setInputPin(1, dc.getOut(0x31));
		ldV.setInputPin(2, dc.getOut(0x32));
		ldV.setInputPin(3, dc.getOut(0x33));
		ldV.setInputPin(4, dc.getOut(0x34));
		ldV.setInputPin(5, dc.getOut(0x54));
		
		clPSWI.setInputPin(0, dc.getOut(0x1B));
		clPSWI.setInputPin(1, dc.getOut(0x3A));
		
		clPSWT.setInputPin(0, dc.getOut(0x1D));
		clPSWT.setInputPin(1, dc.getOut(0x3A));
		
		ldBR.setInputPin(0, dc.getOut(0x47));
		ldBR.setInputPin(1, dc.getOut(0x48));
		ldBR.setInputPin(2, dc.getOut(0x49));
		ldBR.setInputPin(3, dc.getOut(0x4A));
		ldBR.setInputPin(4, dc.getOut(0x4B));
		ldBR.setInputPin(5, dc.getOut(0x4C));
		
		IVTPout.setInputPin(0, dc.getOut(0x2B));
		IVTPout.setInputPin(1, dc.getOut(0x4D));
		
		UINTout.setInputPin(0, dc.getOut(0x48));
		UINTout.setInputPin(1, dc.getOut(0x49));
		UINTout.setInputPin(2, dc.getOut(0x4A));
		UINTout.setInputPin(3, dc.getOut(0x4B));
		
	}
	
	
	///////////////////////////////////// BUS

	public static Pin mxMDR0() {
		return mxMDR0.getOut(0);
	}

	public static Pin ldMDR() {		//namerno
		return mxMDR0.getOut(0);
	}
	
	public static Pin ldMAR() {	
		return ldMAR.getOut(0);
	}

	public static Pin incMAR() {
		return incMar.getOut(0);
	}

	public static Pin ldDWH() {
		return ldDWH.getOut(0);
	}

	public static Pin ldDWL() {
		return dc.getOut(0x0F);
	}

	public static Pin rdCPU() {
		return dc.getOut(0x2);
	}
	
	public static Pin eMAR() {
		return eMAR.getOut(0);
	}

	public static Pin MDRout() {
		return MDRout.getOut(0);
	}

	public static Pin eMDR() {
		return dc.getOut(0x23);
	}

	public static Pin wrCPU() {
		return dc.getOut(0x23);
	}

	public static Pin DWout() {
		return DWout.getOut(0);
	}
	
	////////////////////////////////////////// FETCH

	public static Pin incPC() {
		return dc.getOut(0x01);
	}

	public static Pin ldPC() {
		return ldPC.getOut(0);
	}

	public static Pin ldIR0() {
		return dc.getOut(0x03);
	}

	public static Pin ldIR1() {
		return dc.getOut(0x05);
	}

	public static Pin ldIR2() {
		return dc.getOut(0x07);
	}

	public static Pin ldIR3() {
		return dc.getOut(0x08);
	}

	public static Pin PCout() {
		return PCout.getOut(0);
	}

	public static Pin PCHout() {
		return dc.getOut(0x40);
	}

	public static Pin IRDAout() {
		return IRDAout.getOut(0);
	}

	public static Pin IRJAout() {
		return IRJAout.getOut(0);
	}

	public static Pin IRPOMout() {
		return IRPOMout.getOut(0);
	}

	public static Pin IRBRout() {
		return dc.getOut(0x3B);
	}

	public static Pin ldGPRAR() {
		return dc.getOut(0x05);
	}

	public static Pin incGPRAR() {
		return dc.getOut(0x14);
	}

	public static Pin incSP() {
		return dc.getOut(0x2A);
	}

	public static Pin decSP() {
		return dc.getOut(0x29);
	}

	public static Pin ldSP() {
		return dc.getOut(0x30);
	}

	public static Pin ldCW() {
		return ldCW.getOut(0);
	}

	public static Pin ldCW1() {
		return ldCW1.getOut(0);
	}

	public static Pin wrGPR() {
		return wrGPR.getOut(0);
	}

	public static Pin SPout() {
		return SPout.getOut(0);
	}

	public static Pin ADDout() {
		return ADDout.getOut(0);
	}

	public static Pin GPRout() {
		return GPRout.getOut(0);
	}

	/////////////////////////////////////////////// Exec1
	
	public static Pin ldAB() {
		return ldAB.getOut(0);
	}

	public static Pin shr() {
		return dc.getOut(0x39);
	}

	public static Pin shl() {
		return dc.getOut(0x51);
	}

	public static Pin ldBB() {
		return ldBB.getOut(0);
	}

	public static Pin ldAW() {
		return ldAW.getOut(0);
	}

	public static Pin ldBW() {
		return ldBW.getOut(0);
	}
	
	public static Pin inc() {
		return dc.getOut(0x33);
	}
	
	public static Pin dec() {
		return dc.getOut(0x34);
	}

	public static Pin add() {
		return dc.getOut(0x31);
	}

	public static Pin sub() {
		return dc.getOut(0x32);
	}

	public static Pin or() {
		return dc.getOut(0x36);
	}

	public static Pin and() {
		return dc.getOut(0x35);
	}

	public static Pin xor() {
		return dc.getOut(0x37);
	}

	public static Pin not() {
		return dc.getOut(0x38);
	}

	public static Pin ABout() {
		return ABout.getOut(0);
	}

	public static Pin ALUout() {
		return ALUout.getOut(0);
	}

	public static Pin BBout() {
		return dc.getOut(0x1F);
	}

	public static Pin AWHout() {
		return dc.getOut(0x25);
	}

	public static Pin AWout() {
		return AWout.getOut(0);
	}

	public static Pin BWout() {
		return dc.getOut(0x21);
	}
	
	//////////////////////////////////////////////////

	public static Pin ldIMR() {
		return dc.getOut(0x2E);
	}

	public static Pin IMRout() {
		return dc.getOut(0x2D);
	}
	
	///////////////////////////////////////////////////
	
	public static Pin ldPSWH() {
		return dc.getOut(0x43);
	}

	public static Pin stPSWI() {
		return dc.getOut(0x1C);
	}

	public static Pin stPSWT() {
		return dc.getOut(0x1E);
	}

	public static Pin clPSWI() {
		return clPSWI.getOut(0);
	}

	public static Pin clPSWT() {
		return clPSWT.getOut(0);
	}

	public static Pin ldPSWL() {
		return dc.getOut(0x42);
	}

	public static Pin ldN() {
		return ldN.getOut(0);
	}

	public static Pin ldZ() {		// namerno
		return ldN.getOut(0);
	}

	public static Pin ldC() {
		return ldC.getOut(0);
	}

	public static Pin ldV() {
		return ldV.getOut(0);
	}

	public static Pin ldL() {
		return dc.getOut(0x4C);
	}

	public static Pin PSWHout() {
		return dc.getOut(0x45);
	}

	public static Pin PSWLout() {
		return dc.getOut(0x46);
	}

	public static Pin clSTART() {
		return dc.getOut(0x1A);
	}
	
	/////////////////////////////////////////////////

	public static Pin stPRINS() {
		return dc.getOut(0x3F);
	}

	public static Pin clPRINS() {
		return dc.getOut(0x47);
	}

	public static Pin stPRCOD() {
		return dc.getOut(0x04);
	}

	public static Pin clPRCOD() {
		return dc.getOut(0x49);
	}

	public static Pin stPRADR() {
		return dc.getOut(0x06);
	}

	public static Pin clPRADR() {
		return dc.getOut(0x4A);
	}

	public static Pin clPRINM() {
		return dc.getOut(0x4B);
	}

	public static Pin clINTR() {
		return dc.getOut(0x4C);
	}
	
	/////////////////////////////////////////////////////

	public static Pin ldBR() {
		return ldBR.getOut(0);
	}

	public static Pin ldIVTP() {
		return dc.getOut(0x2C);
	}

	public static Pin UINTout() {
		return UINTout.getOut(0);
	}

	public static Pin UEXTout() {
		return dc.getOut(0x4C);
	}

	public static Pin IVTDSPout() {
		return dc.getOut(0x4E);
	}

	public static Pin IVTPout() {
		return IVTPout.getOut(0);
	}
	

	
	public static void main(String[] args){

		/*
		String fileName = "./src/MicroMemoryContentTEST" ;
		
		int counter=0;
		try {
			BufferedReader reader = new BufferedReader(new FileReader(fileName));
			String line = null;
			while ((line = reader.readLine()) != null) {
			
				
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		*/
		
	}


}
